package tpo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import dto.Dto;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pojo.BuyOrder;
import pojo.Journal;
import pojo.Pay_order;
import pojo.Receipt_order;
import pojo.finance.Day;
import pojo.finance.Fukuandan;
import pojo.finance.Kehuduizhang;
import pojo.finance.Zijinduizhang;
import tpo.dao.Receipt_order_Mapper;
import tpo.service.Receipt_order_Service;

import javax.annotation.Resource;
import java.beans.Transient;
import java.math.BigDecimal;
import java.util.*;

@Service
public class Receipt_order_ServiceImpl implements Receipt_order_Service {

    @Resource
    private Receipt_order_Mapper receipt_order_mapper;

    @Override
    public List<Receipt_order> allreceipt_order(Map<String, Object> map) {
//        List list=receipt_order_mapper.allreceipt_order(map);
        return receipt_order_mapper.allreceipt_order(map);
    }

    @Override
    public List<Receipt_order> allReceipt_order() {
        return receipt_order_mapper.allReceipt_order();
    }

    @Override
    public Receipt_order selectReceipt_order(String code) {
        return receipt_order_mapper.selectReceipt_order(code);
    }

    @Override
    public BigDecimal selectallReceipt_ordermoney(String code) {
        return receipt_order_mapper.selectallReceipt_ordermoney(code);
    }

    @Override
    public Integer addReceipt_order(Receipt_order receipt_order) {
        return receipt_order_mapper.addReceipt_order(receipt_order);
    }

    @Override
    @Transactional
    public Integer zuofei(Map<String,Object> map) {
        Integer receiptSta=(Integer) map.get("receiptSta");
        String code=(String) map.get("code");
        Integer num=0;
        if (receiptSta==15){
            if (receipt_order_mapper.updatesouzisoukuandan(code)>0&&receipt_order_mapper.zuofei(map)>0&& receipt_order_mapper.jianmoney(map)>0){
                    num=1;
            }
        }else{
            num=receipt_order_mapper.zuofei(map);
        }
        return num;
    }

    @Override
    @Transactional
    public Integer shenhe(Map<String, Object> map) {
        Integer num=0;
        map.put("code","ls-"+Num());
        if(receipt_order_mapper.shenhe(map)>0&&receipt_order_mapper.insertlsdan(map)>0&&receipt_order_mapper.addmoney(map)>0){
            num=1;
        }
        return num;
    }

    @Override
    public List<Journal> alljournal(Map<String, Object> map) {
        return receipt_order_mapper.alljournal(map);
    }

    @Override
    public Dto<PageInfo<Fukuandan>> allPay_order(Map<String, Object> map) {
        Integer pageSize=Integer.parseInt(map.get("pageSize").toString());
        Integer pageNum=Integer.parseInt(map.get("pageNun").toString());
        //页面的页码标签数
        int navigatePages=5;
        PageHelper.startPage(pageNum,pageSize);//开始分页,设参数
        List<Fukuandan> list =receipt_order_mapper.allPay_order(map);//查询数据
        PageInfo<Fukuandan> pageInfo = new PageInfo<>(list,navigatePages);//数据放入自动参数分页
        Dto dto=new Dto();
        dto.setSuccess(true);
        dto.setObj(pageInfo);
        return dto;
    }

    @Override
    public Dto addPay_order(Pay_order pay_order) {
        Dto dto=new Dto();
        dto.setSuccess(true);
        dto.setObj(receipt_order_mapper.addPay_order(pay_order));
        return dto;
    }

    @Override
    @Transactional
    public Dto fukuanzuofei(Map<String,Object> map) {
        Integer num=0;
        Dto dto=new Dto();
        Integer receiptSta=(Integer) map.get("status");
        dto.setSuccess(true);
        String code=(String) map.get("code");
        if (receiptSta==15){
            if (receipt_order_mapper.updatesouzisoukuandan(code)>0&&receipt_order_mapper.fukuanzuofei(map)>0&&receipt_order_mapper.addmoney(map)>0){
                num=1;
            }
        }else{
            num=receipt_order_mapper.fukuanzuofei(map);
        }
        dto.setObj(num);
        return dto;
    }

    @Override
    public Dto selectpay_order(String code) {
        Dto dto=new Dto();
        dto.setSuccess(true);
        dto.setObj(receipt_order_mapper.selectpay_order(code));
        return dto;
    }

    @Override
    @Transactional
    public Dto fukuanshenhe(Map<String, Object> map) {
        Integer num=0;
        Dto dto=new Dto();
        dto.setSuccess(true);
        map.put("receiptId","ls-"+Num());
        map.put("checkDate",new Date());
        if(receipt_order_mapper.fukuanshenhe(map)>0&&receipt_order_mapper.insertfukuanlsdan(map)>0&&receipt_order_mapper.jianmoney(map)>0){
            num=1;
        }
        dto.setObj(num);
        return dto;
    }

    @Override
    public Dto<PageInfo<Kehuduizhang>> selectusermoney(Map<String, Object> map) {
        Dto dto=new Dto();
        Integer pageNum=Integer.parseInt(map.get("pageNun").toString());
        Integer pageSize=Integer.parseInt(map.get("pageSize").toString());
        dto.setSuccess(true);
        //页面的页码标签数
        int navigatePages=5;
        PageHelper.startPage(pageNum,pageSize);//开始分页,设参数
        List<Kehuduizhang> list =receipt_order_mapper.selectusermoney(map);//查询数据
        PageInfo<Kehuduizhang> pageInfo = new PageInfo<>(list,navigatePages);//数据放入自动参数分页
        dto.setObj(pageInfo);
        return dto;
    }

    @Override
    public Dto<Kehuduizhang> selectusersum() {
        Dto<Kehuduizhang> dto=new Dto<>();
        dto.setObj(receipt_order_mapper.selectusersum());
        dto.setSuccess(true);
        return dto;
    }

    @Override
    public Dto<Kehuduizhang> selectoneusersum(String id) {
        Dto<Kehuduizhang> dto=new Dto<>();
        dto.setObj(receipt_order_mapper.selectoneusersum(id));
        dto.setSuccess(true);
        return dto;
    }

    @Override
    public Dto<PageInfo<BuyOrder>> selectoneuseralldan(Map<String, Object> map) {

        Integer pageNum=Integer.parseInt(map.get("pageNun").toString());
        Dto dto=new Dto();
        Integer pageSize=Integer.parseInt(map.get("pageSize").toString());
        //页面的页码标签数
        int navigatePages=5;
        PageHelper.startPage(pageNum,pageSize);//开始分页,设参数
        List<BuyOrder> list =receipt_order_mapper.selectoneuseralldan(map);//查询数据
        PageInfo<BuyOrder> pageInfo = new PageInfo<>(list,navigatePages);//数据放入自动参数分页
        dto.setSuccess(true);
        dto.setObj(pageInfo);
        return dto;
    }

    @Override
    public Dto<PageInfo<Zijinduizhang>> selectdangtianzhang(Map<String,Object> map) {
        Dto dto=new Dto();
        Integer pageNum=Integer.parseInt(map.get("pageNun").toString());
        Integer pageSize=Integer.parseInt(map.get("pageSize").toString());
        //页面的页码标签数
        int navigatePages=5;
        PageHelper.startPage(pageNum,pageSize);//开始分页,设参数
        List<Zijinduizhang> list =receipt_order_mapper.selectdangtianzhang();//查询数据
        PageInfo<Zijinduizhang> pageInfo = new PageInfo<>(list,navigatePages);//数据放入自动参数分页
        dto.setSuccess(true);
        dto.setObj(pageInfo);
        return dto;
    }

    @Override
    public Dto<PageInfo<Journal>> selectzijinmingxi(Map<String, Object> map) {
        Integer pageNum=Integer.parseInt(map.get("pageNun").toString());
        Integer pageSize=Integer.parseInt(map.get("pageSize").toString());
        Dto dto=new Dto();
        //页面的页码标签数
        int navigatePages=5;
        PageHelper.startPage(pageNum,pageSize);//开始分页,设参数
        List<Journal> list =receipt_order_mapper.selectzijinmingxi(map);//查询数据
        PageInfo<Journal> pageInfo = new PageInfo<>(list,navigatePages);//数据放入自动参数分页
        dto.setSuccess(true);
        dto.setObj(pageInfo);
        return dto;
    }

    @Override
    public Dto<List<Day>> zijinzhexiantu() {
        Dto<List<Day>> dto=new Dto<>();
        dto.setObj(receipt_order_mapper.zijinzhexiantu());
        dto.setSuccess(true);
        return dto;
    }

    @Override
    public Dto<List<Receipt_order>> codeselectallreceipt(String code) {
        Dto<List<Receipt_order>> dto=new Dto<>();
        dto.setObj(receipt_order_mapper.codeselectallreceipt(code));
        dto.setSuccess(true);
        return dto;
    }

    @Override
    public Dto<List<Pay_order>> codeselectallfukuandan(String code) {
        Dto<List<Pay_order>> dto=new Dto<>();
        dto.setObj(receipt_order_mapper.codeselectallfukuandan(code));
        dto.setSuccess(true);
        return dto;
    }

    @Override
    public Dto selectallokfukuandan(String code) {
        Dto dto=new Dto<>();
        dto.setObj(receipt_order_mapper.selectallokfukuandan(code));
        dto.setSuccess(true);
        return dto;
    }

    @Override
    public Dto upPay_order(Map<String, Object> map) {

        Dto dto=new Dto();
        dto.setSuccess(true);
        dto.setObj(receipt_order_mapper.upPay_order(map));
        return dto;
    }


    //随机生成6位数
     static int Num() {
        int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        Random rand = new Random();
        for (int i = 10; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = array[index];
            array[index] = array[i - 1];
            array[i - 1] = tmp;
        }
        int result = 0;
        for (int i = 0; i < 6; i++) {
            result = result * 10 + array[i];
        }
        if (String.valueOf(result).length() == 6) {
            return result;
        } else {
            return Num();
        }
    }

}
